using System;
using Spread.Common.Features.Sample;
using GrapeCity.Spreadsheet;

namespace Spread.Calculation.Features
{
    public partial class CrossWorkbookReferenceDemo : BaseSampleControl
    {
        IWorkbookSet workbookSet = GrapeCity.Spreadsheet.Win.Factory.CreateWorkbookSet();

        public CrossWorkbookReferenceDemo()
        {
            InitializeComponent();
            SetupSpreadsheets();
        }

        private void SetupSpreadsheets()
        {
            // create first workbook
            IWorkbook wb1 = workbookSet.Workbooks.Add();
            for (int i = 0; i < 3; i++)
                wb1.Worksheets.Add();
            fpSpread1.Attach(wb1);
            fpSpread1.TitleInfo.Text = "Book1";
            fpSpread1.TitleInfo.Visible = true;
            fpSpread1.Sheets[0].Cells[0, 0].Text = "Northern Territory";
            fpSpread1.Sheets[0].Cells[1, 0].Text = "Total Unit Sales";
            fpSpread1.Sheets[0].Cells[1, 1].Formula = "SUM(B3:B9)";
            fpSpread1.Sheets[0].Cells[2, 0].Text = "10/1/2018";
            fpSpread1.Sheets[0].Cells[2, 1].Value = 52;
            fpSpread1.Sheets[0].Cells[3, 0].Text = "10/2/2018";
            fpSpread1.Sheets[0].Cells[3, 1].Value = 98;
            fpSpread1.Sheets[0].Cells[4, 0].Text = "10/3/2018";
            fpSpread1.Sheets[0].Cells[4, 1].Value = 27;
            fpSpread1.Sheets[0].Cells[5, 0].Text = "10/4/2018";
            fpSpread1.Sheets[0].Cells[5, 1].Value = 118;
            fpSpread1.Sheets[0].Cells[6, 0].Text = "10/5/2018";
            fpSpread1.Sheets[0].Cells[6, 1].Value = 91;
            fpSpread1.Sheets[0].Cells[7, 0].Text = "10/6/2018";
            fpSpread1.Sheets[0].Cells[7, 1].Value = 63;
            fpSpread1.Sheets[0].Cells[8, 0].Text = "10/7/2018";
            fpSpread1.Sheets[0].Cells[8, 1].Value = 74;
            fpSpread1.StatusBarVisible = true;
            // create second workbook
            IWorkbook wb2 = workbookSet.Workbooks.Add();
            for (int i = 0; i < 3; i++)
                wb2.Worksheets.Add();
            fpSpread2.Attach(wb2);
            fpSpread2.TitleInfo.Text = "Book2";
            fpSpread2.TitleInfo.Visible = true;
            fpSpread2.Sheets[0].Cells[0, 0].Text = "Region";
            fpSpread2.Sheets[0].Cells[0, 1].Text = "Total Units";
            fpSpread2.Sheets[0].Cells[1, 0].Text = "Northern Territory";
            fpSpread2.Sheets[0].Cells[1, 1].Formula = "[Book1]Sheet1!$B$2";
            fpSpread2.Sheets[0].Cells[2, 0].Text = "Southern Territory";
            fpSpread2.Sheets[0].Cells[2, 1].Value = 725;
            fpSpread2.Sheets[0].Cells[3, 0].Text = "Eastern Territory";
            fpSpread2.Sheets[0].Cells[3, 1].Value = 437;
            fpSpread2.Sheets[0].Cells[4, 0].Text = "Western Territory";
            fpSpread2.Sheets[0].Cells[4, 1].Value = 623;
            fpSpread2.Sheets[0].AddTable("table1", 0, 0, 5, 2);
            fpSpread2.StatusBarVisible = true;

            wb1.ActiveSheet.Columns[0].AutoFit();
            fpSpread2.ActiveSheet.SetActiveCell(1, 1);
            wb2.SaveLinkValues = true;
            fpSpread2_Enter(null, EventArgs.Empty);
            splitContainer1.SplitterDistance = splitContainer2.SplitterDistance;
        }

        private void fpSpread1_Enter(object sender, EventArgs e)
        {
            formulaTextBox1.Detach();
            formulaTextBox1.Attach(fpSpread1);
            nameBox1.Detach();
            nameBox1.Attach(fpSpread1);
        }

        private void fpSpread2_Enter(object sender, EventArgs e)
        {
            formulaTextBox1.Detach();
            formulaTextBox1.Attach(fpSpread2);
            nameBox1.Detach();
            nameBox1.Attach(fpSpread2);
        }

        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);
            if (Visible)
                fpSpread2.Focus();
        }
    }
}


//
//CrossWorkbookReferenceDemo.Designer.cs
//


namespace Spread.Calculation.Features
{
    partial class CrossWorkbookReferenceDemo
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.panelFormulaBar = new System.Windows.Forms.Panel();
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.nameBox1 = new FarPoint.Win.Spread.NameBox();
            this.formulaTextBox1 = new FarPoint.Win.Spread.FormulaTextBox();
            this.splitContainer2 = new System.Windows.Forms.SplitContainer();
            this.fpSpread1 = new FarPoint.Win.Spread.FpSpread(FarPoint.Win.Spread.LegacyBehaviors.None);
            this.fpSpread2 = new FarPoint.Win.Spread.FpSpread(FarPoint.Win.Spread.LegacyBehaviors.None);
            this.panelFormulaBar.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).BeginInit();
            this.splitContainer2.Panel1.SuspendLayout();
            this.splitContainer2.Panel2.SuspendLayout();
            this.splitContainer2.SuspendLayout();
            this.pnlMain.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1)).BeginInit();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.splitContainer2);
            this.pnlMain.Controls.Add(this.panelFormulaBar);
            // 
            // panelFormulaBar
            // 
            this.panelFormulaBar.Controls.Add(this.splitContainer1);
            this.panelFormulaBar.Dock = System.Windows.Forms.DockStyle.Top;
            this.panelFormulaBar.Location = new System.Drawing.Point(0, 24);
            this.panelFormulaBar.Name = "panelFormulaBar";
            this.panelFormulaBar.Size = new System.Drawing.Size(800, 23);
            this.panelFormulaBar.TabIndex = 6;
            // 
            // splitContainer1
            // 
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.Location = new System.Drawing.Point(0, 0);
            this.splitContainer1.Name = "splitContainer1";
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this.nameBox1);
            this.splitContainer1.Panel1MinSize = 121;
            // 
            // splitContainer1.Panel2
            // 
            this.splitContainer1.Panel2.Controls.Add(this.formulaTextBox1);
            this.splitContainer1.Size = new System.Drawing.Size(800, 23);
            this.splitContainer1.SplitterDistance = 121;
            this.splitContainer1.TabIndex = 0;
            // 
            // nameBox1
            // 
            this.nameBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.nameBox1.Location = new System.Drawing.Point(0, 0);
            this.nameBox1.MaxLength = 255;
            this.nameBox1.Name = "nameBox1";
            this.nameBox1.Size = new System.Drawing.Size(121, 21);
            this.nameBox1.TabIndex = 0;
            // 
            // formulaTextBox1
            // 
            this.formulaTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.formulaTextBox1.Location = new System.Drawing.Point(0, 0);
            this.formulaTextBox1.Multiline = false;
            this.formulaTextBox1.Name = "formulaTextBox1";
            this.formulaTextBox1.Size = new System.Drawing.Size(675, 23);
            this.formulaTextBox1.TabIndex = 0;
            // 
            // splitContainer2
            // 
            this.splitContainer2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer2.Location = new System.Drawing.Point(0, 0);
            this.splitContainer2.Name = "splitContainer2";
            // 
            // splitContainer2.Panel1
            // 
            this.splitContainer2.Panel1.Controls.Add(this.fpSpread1);
            // 
            // splitContainer2.Panel2
            // 
            this.splitContainer2.Panel2.Controls.Add(this.fpSpread2);
            this.splitContainer2.Size = new System.Drawing.Size(800, 450);
            this.splitContainer2.TabIndex = 0;
            // 
            // fpSpread1
            // 
            this.fpSpread1.AccessibleDescription = "";
            this.fpSpread1.BackColor = System.Drawing.SystemColors.Control;
            this.fpSpread1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.fpSpread1.Location = new System.Drawing.Point(0, 0);
            this.fpSpread1.Name = "fpSpread1";
            this.fpSpread1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.fpSpread1.Size = new System.Drawing.Size(396, 450);
            this.fpSpread1.TabIndex = 1;
            this.fpSpread1.Enter += new System.EventHandler(this.fpSpread1_Enter);
            // 
            // fpSpread2
            // 
            this.fpSpread2.AccessibleDescription = "";
            this.fpSpread2.BackColor = System.Drawing.SystemColors.Control;
            this.fpSpread2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.fpSpread2.Location = new System.Drawing.Point(0, 0);
            this.fpSpread2.Name = "fpSpread2";
            this.fpSpread2.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.fpSpread2.Size = new System.Drawing.Size(400, 450);
            this.fpSpread2.TabIndex = 1;
            this.fpSpread2.Enter += new System.EventHandler(this.fpSpread2_Enter);
            // 
            // CrossWorkbookReferenceDemo
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "CrossWorkbookReferenceDemo";
            this.panelFormulaBar.ResumeLayout(false);
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
            this.splitContainer1.ResumeLayout(false);
            this.splitContainer2.Panel1.ResumeLayout(false);
            this.splitContainer2.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit();
            this.splitContainer2.ResumeLayout(false);
            this.pnlMain.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread2)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion
        private System.Windows.Forms.SplitContainer splitContainer1;
        protected internal System.Windows.Forms.Panel panelFormulaBar;
        protected internal FarPoint.Win.Spread.NameBox nameBox1;
        protected internal FarPoint.Win.Spread.FormulaTextBox formulaTextBox1;
        private System.Windows.Forms.SplitContainer splitContainer2;
        protected internal FarPoint.Win.Spread.FpSpread fpSpread1;
        protected internal FarPoint.Win.Spread.FpSpread fpSpread2;
    }
}
